﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MyDLL
{
    public class String_N_Byte
    {
        /// <summary>
        /// 将字符串按byte数分隔，返回字符串数组。
        /// </summary>
        /// <param name="str0">原字符串</param>
        /// <param name="splitArr">分隔数组</param>
        /// <param name="EncodingStr">编码，默认为"GBK"</param>
        /// <returns>返回字符串数组</returns>
        public string[] StringSplit_perByte(string str0, int[] splitArr, string EncodingStr = "GBK")
        {
            string[] strArr = new string[splitArr.Length];
            byte[] bytes = Encoding.GetEncoding(EncodingStr).GetBytes(str0);
            List<byte[]> byteTempList = new List<byte[]>();
            int splitCount = 0;

            for (int i = 0; i < splitArr.Length; i++)
            {
                byte[] byteArr = new byte[splitArr[i]];
                for (int j = 0; j < byteArr.Length; j++)
                {
                    byteArr[j] = bytes[splitCount + j];
                }
                byteTempList.Add(byteArr);
                splitCount += splitArr[i];
            }

            for (int i = 0; i < byteTempList.Count; i++)
            {
                strArr[i] = Encoding.GetEncoding(EncodingStr).GetString(byteTempList[i]);
            }

            return strArr;
        }

        /// <summary>
        /// 返回字符串的byte长度。
        /// </summary>
        /// <param name="str0">原字符串</param>
        /// <param name="EncodingStr">编码，默认为"GBK"</param>
        /// <returns>返回byte长度</returns>
        public int GetByteLength(string str0, string EncodingStr)
        {
            return Encoding.GetEncoding(EncodingStr).GetBytes(str0).Length;
        }
    }
}
